﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kardex_Entity;

namespace Kardex_DataAccess
{
    public class ClientDA
    {
           bd_kardexEntities context;
           public ClientDA()
         {
             context = new bd_kardexEntities();
         }
           
           public List<person> GetListClient()
           {

               List<person> ListClient = null;
               try
               {
                   var q = from c in context.client
                           from p in context.person
                           where p.IdPerson == c.IdPerson 
                           orderby p.IdPerson
                           select p ;
                   ListClient = q.ToList();

                   return ListClient;
               }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message.ToString());
               }
           }
           public person GetObjByIdperson(int IdPerson)
           {
               try
               {
                   var q = from c in context.client
                           from p in context.person
                           where p.IdPerson == c.IdPerson &&
                           c.IdPerson == IdPerson
                           orderby p.IdPerson
                           select p;
                   person objPerson = (person)q.FirstOrDefault();
                   return objPerson;
               }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message.ToString());
               }
           }
           public person Save(person NewPerson)
           {
               try
               {
                   context.person.AddObject(NewPerson);
                   context.SaveChanges();
               }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message.ToString());
               }
               return NewPerson;
           }
          
           public bool Udate(person PersonModif)
           {
               bool ReturnResult = false;
               try
               {
                   context.person.Attach(PersonModif);
                   context.ObjectStateManager.ChangeObjectState(PersonModif, System.Data.EntityState.Modified);
                   context.SaveChanges();

                   context.client.Attach(PersonModif.client);
                   context.ObjectStateManager.ChangeObjectState(PersonModif.client, System.Data.EntityState.Modified);
                   context.SaveChanges();

                   ReturnResult = true;
               }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message.ToString());
               }
               return ReturnResult;
           }
           
           public bool Delete(person PersonDelete)
           {
               bool ReturnResult = false;
               try
               {
                   context.person.Attach(PersonDelete);
                   context.DeleteObject(PersonDelete);
                   context.SaveChanges();
                   ReturnResult = true;
               }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message.ToString());
               }
               return ReturnResult;
           }

    }
}
